package com.ruoyi.web.controller.wx;

import java.lang.reflect.Array;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.ruoyi.info.domain.VinfoCar;
import com.ruoyi.info.service.IVinfoCarService;
import com.ruoyi.web.controller.util.RequestUtil;
import com.ruoyi.wx.domain.Vehicles;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wx.domain.VehiclesLog;
import com.ruoyi.wx.service.IVehiclesLogService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 车辆日志Controller
 *
 * @author oy
 * @date 2022-12-13
 */
@RestController
@RequestMapping("/wx/vehiclelog")
public class VehiclesLogController extends BaseController
{
    @Resource
    private IVehiclesLogService vehiclesLogService;

    @Resource
    IVinfoCarService carService;

    /**
     * 查询车辆日志列表
     */
    @PreAuthorize("@ss.hasPermi('wx:vehiclelog:list')")
    @GetMapping("/list")
    public TableDataInfo list(VehiclesLog vehiclesLog)
    {
        startPage();
        List<VehiclesLog> list = vehiclesLogService.selectVehiclesLogList(vehiclesLog);
        return getDataTable(list);
    }

    /**
     * 导出车辆日志列表
     */
    @PreAuthorize("@ss.hasPermi('wx:vehiclelog:export')")
    @Log(title = "车辆日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VehiclesLog vehiclesLog)
    {
        List<VehiclesLog> list = vehiclesLogService.selectVehiclesLogList(vehiclesLog);
        ExcelUtil<VehiclesLog> util = new ExcelUtil<VehiclesLog>(VehiclesLog.class);
        util.exportExcel(response, list, "车辆日志数据");
    }

    /**
     * 获取车辆日志详细信息
     */
    @PreAuthorize("@ss.hasPermi('wx:vehiclelog:query')")
    @GetMapping(value = "/{vehicleLogId}")
    public AjaxResult getInfo(@PathVariable("vehicleLogId") Long vehicleLogId)
    {
        return success(vehiclesLogService.selectVehiclesLogByVehicleLogId(vehicleLogId));
    }

    /**
     * 新增车辆日志
     */
    @PreAuthorize("@ss.hasPermi('wx:vehiclelog:add')")
    @Log(title = "车辆日志", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VehiclesLog vehiclesLog)
    {
        String[] alarmList = vehiclesLog.getAlarmList();
        String alarmLists = "";
        for (int i = 0; i < alarmList.length; i++){
            if (i == 0){
                alarmLists += alarmList[i];
            }else {
                alarmLists += ",";
                alarmLists += alarmList[i];
            }
        }
        return toAjax(vehiclesLogService.insertVehiclesLog(vehiclesLog));
    }

    /**
     * 修改车辆日志
     */
    @PreAuthorize("@ss.hasPermi('wx:vehiclelog:edit')")
    @Log(title = "车辆日志", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VehiclesLog vehiclesLog)
    {
        return toAjax(vehiclesLogService.updateVehiclesLog(vehiclesLog));
    }

    /**
     * 删除车辆日志
     */
    @PreAuthorize("@ss.hasPermi('wx:vehiclelog:remove')")
    @Log(title = "车辆日志", businessType = BusinessType.DELETE)
	@DeleteMapping("/{vehicleLogIds}")
    public AjaxResult remove(@PathVariable Long[] vehicleLogIds)
    {
        return toAjax(vehiclesLogService.deleteVehiclesLogByVehicleLogIds(vehicleLogIds));
    }

    /**
     * 定时获取车辆日志信息并存储到数据库
     */
    public void getAndputToDB(){
        RequestUtil requestUtil = new RequestUtil();
        List<String> deviceNoList = carService.selectDeviceNos();
        String url = "https://api.ly-gps.com/lygps-api/v2/locate/getDeviceLocate";
        Map<String,Object> params = new HashMap<>();
        params.put("deviceNoList",deviceNoList);
        AjaxResult res = requestUtil.post(url,params);
        if (res.get("code").toString().equals("200")){
            String data = res.get("data").toString();
            List<VehiclesLog> vehiclesLogList = JSON.parseObject(data, new TypeReference<ArrayList<VehiclesLog>>(){});
            for (VehiclesLog vehiclesLog : vehiclesLogList){
                vehiclesLogService.insertVehiclesLog(vehiclesLog);
            }
        }
    }
}
